package com.zshan.clinic.web.controller.medical;


import com.zshan.clinic.admin.entity.dto.ClinicMedicalDTO;
import com.zshan.clinic.admin.entity.vo.*;
import com.zshan.clinic.admin.entity.vo.print.ClinicExamPrintVo;
import com.zshan.clinic.admin.entity.vo.print.ClinicMedicalPrintVo;
import com.zshan.clinic.admin.entity.vo.print.ClinicRxPrintVo;
import com.zshan.clinic.admin.service.medical.ClinicFollowupService;
import com.zshan.clinic.admin.service.medical.ClinicMedicalService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * 门诊
 * @author lichen
 */
@RestController
@RequestMapping("/clinic/medical")
public class ClinicMedicalController extends AbstractController {


    @Autowired
    private ClinicMedicalService clinicMedicalService;

    @Autowired
    private ClinicFollowupService clinicFollowupService;
    /**
     * 打印：处方
     * @param medicalId 门诊ID
     * @param isAll 是否打印全部，默认是0（只打印未缴费的），如果门诊已经是完成状态需要补打查验申请单则给1
     * @return
     */
    @GetMapping("/getClinicRxPrint")
    public RestResp<ClinicRxPrintVo> getClinicRxPrint(@RequestParam(name = "medicalId") Long medicalId,
                                                      @RequestParam(name = "isAll",defaultValue = "0") String isAll) {
        UserInfo userInfo = getUserInfo();
        ClinicRxPrintVo clinicRxPrintVo = clinicMedicalService.getClinicRxPrint(userInfo.getClinicId(), medicalId,isAll,userInfo.getEmpName());
        return RestResp.SUCCESS(clinicRxPrintVo);
    }

    /**
     * 打印：查验申请单
     * @param medicalId 门诊ID
     * @param isAll 是否打印全部，默认是0（只打印未缴费的），如果门诊已经是完成状态需要补打查验申请单则给1
     * @return
     */
    @GetMapping("/getClinicExamPrint")
    public RestResp<ClinicExamPrintVo> getClinicExamPrint(@RequestParam(name = "medicalId") Long medicalId,
                                                          @RequestParam(name = "isAll",defaultValue = "0") String isAll) {
        UserInfo userInfo = getUserInfo();
        ClinicExamPrintVo clinicExamPrintVo = clinicMedicalService.getClinicExamPrint(userInfo.getClinicId(), medicalId,isAll,userInfo.getEmpName());
        return RestResp.SUCCESS(clinicExamPrintVo);
    }

    /**
     * 打印：病历
     * @param medicalId
     * @return
     */
    @GetMapping("/getClinicMedicalPrint")
    public RestResp<ClinicMedicalPrintVo> getClinicMedicalPrint(@RequestParam(name = "medicalId") Long medicalId) {
        UserInfo userInfo = getUserInfo();
        ClinicMedicalPrintVo clinicMedicalPrintVo = clinicMedicalService.getClinicMedicalPrint(userInfo.getClinicId(), medicalId,userInfo.getEmpName());
        return RestResp.SUCCESS(clinicMedicalPrintVo);
    }

    /**
     * 门诊：药品冲突验证-完成诊断按钮之前调用
     * @return
     */
    @PostMapping("/verify")
    public RestResp<List<String>> verify(@RequestBody ClinicMedicalDTO clinicMedicalDTO) {
        UserInfo userInfo = getUserInfo();
        clinicMedicalDTO.setUpdateUserId(userInfo.getEmpId());
        clinicMedicalDTO.setUpdateBy(userInfo.getEmpName());
        clinicMedicalDTO.setClinicId(userInfo.getClinicId());
        clinicMedicalDTO.setDoctorId(userInfo.getEmpId());
        clinicMedicalDTO.setDoctorName(userInfo.getEmpName());
        List<String> conflictList = clinicMedicalService.verify(clinicMedicalDTO);
        return RestResp.SUCCESS(conflictList);
    }

    /**
     * 患者：查询患者病历记录
     * @param patientId 患者ID
     * @param visitDate 就诊日期
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPatientMedicalList")
    public RestResp<PageInfo<ClinicMedicalPatientVo>> getPatientMedicalList(@RequestParam(name = "patientId") Long patientId,
                                                                            @RequestParam(name = "visitDate",required = false) LocalDate visitDate,
                                                                            @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<ClinicMedicalPatientVo> medicalList = clinicMedicalService.getPatientMedicalList(userInfo.getClinicId(), patientId,visitDate,pageNo,pageSize);
        return RestResp.SUCCESS(medicalList);
    }

    /**
     * 门诊：查询诊所今日待诊数
     * @return
     */
    @GetMapping("/getWaitingCount")
    public RestResp<MedicalWaitingVo> getWaitingCount() {
        UserInfo userInfo = getUserInfo();
        MedicalWaitingVo medicalWaitingVo = new MedicalWaitingVo();
        medicalWaitingVo.setMedicalWaitingCount(clinicMedicalService.getWaitingCount(userInfo.getClinicId(),userInfo.getEmpId()));
        medicalWaitingVo.setFollowupPendingCount(clinicFollowupService.getFollowupPendingCount(userInfo.getClinicId(),userInfo.getEmpId()));
        return RestResp.SUCCESS(medicalWaitingVo);
    }

    /**
     * 门诊：查询患者列表
     * @param patientName 患者名
     * @param visitDate 就诊日期 默认当天
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getMedicalList")
    public RestResp<List<ClinicMedicalPageVo>> getMedicalList(@RequestParam(name = "patientName",required = false) String patientName,
                                                              @RequestParam(name = "visitDate",required = false) LocalDate visitDate,
                                                              @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<ClinicMedicalPageVo> medicalList = clinicMedicalService.getMedicalList(userInfo.getClinicId(), patientName,userInfo.getEmpId(),visitDate,pageNo,pageSize);
        return RestResp.SUCCESS(medicalList);
    }

    /**
     * 门诊：查询患者就诊历史数
     * @param patientId 患者ID
     * @return
     */
    @GetMapping("/getRecordCount")
    public RestResp<Long> getRecordCount(@RequestParam(name = "patientId") Long patientId) {
        UserInfo userInfo = getUserInfo();
        Long recordCount = clinicMedicalService.getRecordCount(userInfo.getClinicId(), patientId);
        return RestResp.SUCCESS(recordCount);
    }

    /**
     * 门诊：查询患者就诊历史
     * @param patientId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getRecordList")
    public RestResp<List<MedicalRecordVo>> getRecordList(@RequestParam(name = "patientId") Long patientId,
                                                         @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<MedicalRecordVo> recordList = clinicMedicalService.getRecordList(userInfo.getClinicId(), patientId,pageNo,pageSize);
        return RestResp.SUCCESS(recordList);
    }

    /**
     * 门诊：查询病历详情
     * @param medicalId
     * @return
     */
    @GetMapping("/getMedicalDetail")
    public RestResp<ClinicMedicalDetailVo> getMedicalDetail(@RequestParam(name = "medicalId",required = false) Long medicalId) {
        UserInfo userInfo = getUserInfo();
        ClinicMedicalDetailVo clinicMedicalDetailVo = clinicMedicalService.getMedicalDetail(userInfo.getClinicId(), medicalId);
        return RestResp.SUCCESS(clinicMedicalDetailVo);
    }

    /**
     * 门诊：完成预诊
     * @return
     */
    @PostMapping("/confirmPre")
    public RestResp<Long> confirmPre(@RequestBody ClinicMedicalDTO clinicMedicalDTO) {
        UserInfo userInfo = getUserInfo();
        clinicMedicalDTO.setUpdateUserId(userInfo.getEmpId());
        clinicMedicalDTO.setUpdateBy(userInfo.getEmpName());
        clinicMedicalDTO.setClinicId(userInfo.getClinicId());
        Long medicalId = clinicMedicalService.confirmPre(clinicMedicalDTO);
        return RestResp.SUCCESS(medicalId);
    }

    /**
     * 门诊：完成诊断
     * @return
     */
    @PostMapping("/completed")
    public RestResp<Long> completed(@RequestBody ClinicMedicalDTO clinicMedicalDTO) {
        UserInfo userInfo = getUserInfo();
        clinicMedicalDTO.setUpdateUserId(userInfo.getEmpId());
        clinicMedicalDTO.setUpdateBy(userInfo.getEmpName());
        clinicMedicalDTO.setClinicId(userInfo.getClinicId());
        Long medicalId = clinicMedicalService.completed(clinicMedicalDTO);
        return RestResp.SUCCESS(medicalId);
    }

    /**
     * 门诊：修改病历
     * @return
     */
    @PostMapping("/editMedical")
    public RestResp<Long> editMedical(@RequestBody ClinicMedicalDTO clinicMedicalDTO) {
        UserInfo userInfo = getUserInfo();
        clinicMedicalDTO.setUpdateUserId(userInfo.getEmpId());
        clinicMedicalDTO.setUpdateBy(userInfo.getEmpName());
        clinicMedicalDTO.setClinicId(userInfo.getClinicId());
        Long medicalId = clinicMedicalService.editMedical(clinicMedicalDTO);
        return RestResp.SUCCESS(medicalId);
    }
}
